<?php

/**
 * @param 传入用户ID
 * @return 生成邀请码
 */
function createInvitationCode($user_id)
{
    static $source_string = '67MWX89KLYZB1NE5FCDG3HQA4TUVOPIJ2RS';
    $code = '';
    while ($user_id > 0) {
        $mod = $user_id % 35;
        $user_id = ($user_id - $mod) / 35;
        $code = $source_string[$mod] . $code;
    }
    if (empty($code[3]))
        $code = str_pad($code, 4, '0', STR_PAD_LEFT);
    return $code;
}

/**
 * @param $code 传入邀请码
 * @return float|int 返回用户ID
 */
function deInvitationCode($code)
{
    static $source_string = '67MWX89KLYZB1NE5FCDG3HQA4TUVOPIJ2RS';
    if (strrpos($code, '0') !== false) {
        $code = substr($code, strrpos($code, '0') + 1);
    }
    $len = strlen($code);
    $code = strrev($code);
    $user_id = 0;
    for ($i = 0; $i < $len; $i++) {
        $user_id += strpos($source_string, $code[$i]) * pow(35, $i);
    }
    return $user_id;
}

/**
 * @param $user_avatar  用户头像
 * @return string  可引用用户头像
 */
function getUserAvatar($user_avatar)
{
    if (empty($user_avatar)) {
        $avatar = env('APP_URL') . 'admin/images/avatar_default.jpg';
    } elseif ((strpos($user_avatar, 'http') > -1)) {
        $avatar = $user_avatar;
    } else {
        $avatar = env('APP_IMGURL') . '/avatar/' . $user_avatar;
    }
    return $avatar;
}

/**
 * @param $type
 * @return string
 */
function gettypename($type)
{
    switch ($type){
        case 1:
            return ['typename'=>'3D模型' ,'route'=>'model'];
        case 2:
            return ['typename'=>'SU模型' ,'route'=>'sumodel'];
        case 3:
            return ['typename'=>'施工图' ,'route'=>'sumodel'];
        case 4:
            return ['typename'=>'概念方案' ,'route'=>'sumodel'];
        case 7:
            return ['typename'=>'高清案例' ,'route'=>'sumodel'];
    }
}
